package com.wjjun.customview.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.wjjun.customview.R;

import java.util.ArrayList;
import java.util.List;

public class VScrollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory {


    private static final int FLAG_START_AUTO_SCROLL = 0;

    private int textSize = 16;
    private int padding = 5;
    private int textColor = Color.BLACK;

    private int time = 2500;

    private OnItemClickListener itemClickListener;
    private Context mContext;
    private int currentId = -1;
    private List textList;
    private Handler handler;

    public VScrollTextView(Context context) {
        this(context, null);
        mContext = context;
    }

    public VScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        textList = new ArrayList<>();
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VScrollTextView);
        textSize = typedArray.getDimensionPixelSize(R.styleable.VScrollTextView_vstv_textSize, textSize);
        padding = typedArray.getDimensionPixelSize(R.styleable.VScrollTextView_vstv_text_padding, padding);
        textColor = typedArray.getColor(R.styleable.VScrollTextView_vstv_textColor, textColor);
        time = typedArray.getInteger(R.styleable.VScrollTextView_vstv_time, time);
        typedArray.recycle();

        setFactory(this);
        setTextStillTime();
    }

    /**
     * 间隔时间
     */
    @SuppressLint("HandlerLeak")
    public void setTextStillTime() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case FLAG_START_AUTO_SCROLL:
                        if (textList.size() > 0) {
                            currentId = (currentId + 1) % textList.size();
                            setText(textList.get(currentId).toString());
                        }
                        handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, time);
                        break;
                }
            }
        };
    }

    /**
     * 设置数据源
     *
     * @param titles
     */
    @SuppressWarnings("unchecked")
    public void setTextList(List titles) {
        textList.clear();
        textList.addAll(titles);
        currentId = 0;
        if (textList == null || textList.isEmpty()) {
            return;
        }
        startAutoScroll();
    }

    /**
     * 开始滚动
     */
    public void startAutoScroll() {
        stopAutoScroll();
        handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
    }

    /**
     * 停止滚动
     */
    public void stopAutoScroll() {
        handler.removeMessages(FLAG_START_AUTO_SCROLL);
    }

    @Override
    public View makeView() {
        TextView t = new TextView(mContext);
        t.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
        t.setMaxLines(1);
        t.setPadding(padding, padding, padding, padding);
        t.setTextColor(textColor);
        t.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        t.setClickable(true);
        t.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null && textList.size() > 0 && currentId != -1) {
                    itemClickListener.onItemClick(currentId, textList.get(currentId));
                }
            }
        });
        return t;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAutoScroll();
    }

    /**
     * 设置点击事件监听
     *
     * @param itemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 轮播文本点击监听器
     */
    public interface OnItemClickListener {
        /**
         * 点击回调
         *
         * @param position 当前点击ID
         */
        void onItemClick(int position, Object item);
    }

}

